package com.gitee.wsl.flow.transform

import kotlinx.coroutines.flow.Flow


/**
 * Class holding the context of the [Action] emitted that is being split out into
 * a [Mutation] [Flow].
 *
 * Use typically involves invoking [type] to identify the [Action] stream being transformed, and
 * subsequently invoking [flow] to perform a custom transformation on the split out [Flow].
 */
data class TransformationContext<Action : Any>(
    private val type: Action,
    val backing: Flow<Action>
) {

    /**
     * A convenience for the backing [Flow] of the [Action] subtype  from the parent [Flow]
     */
    @Suppress("unused", "UNCHECKED_CAST", "UnusedReceiverParameter")
    inline val <reified Subtype : Action> Subtype.flow: Flow<Subtype>
        get() = backing as Flow<Subtype>

    /**
     * the first [Action] of the specified type emitted from the parent [Flow]
     */
    fun type() = type
}